import commonapi from '@/api/commonapi'
import http from '@/api/adminapi'
const state = {
    pubinfo: {},
    getaccountdata: {},
    getcoursedata: {},
    getstuaccdata: {},
    getallcourse:{}
};
const actions = {
    async getpubinfo({ commit }) {
        let res = await commonapi.getpub();
        if (res.status == 200) {
            let pub = res.data;
            commit('GETPUBINFO', pub);
        }
    },
    async getaccount({ commit }) {
        let res = await http.getaccount();
        if (res.status == 200) {
            commit('GETACCOUNT', res.data)
        }
    },
    async gettchbyname({ commit }, name) {
        let res = await http.gettchaccbyname({ name: name });
        if (res.status == 200) {
            commit('GETTCHACCBYNAME', res.data)
        }
    },
    deleteaccount({ commit }, index) {
        commit('DELETEACCOUNT', index)
    },
    changeaccountpage({ commit }, page) {
        commit('CHANGEACCOUNTPAGE', page)
    },
    async getcourse({ commit }) {
        let res = await http.getcourse();
        if (res.status == 200) {
            commit('GETCOURSE', res.data)
        }
    },
    deletecourse({ commit }, index) {
        commit('DELETECOURSE', index);
    },
    changecoursepage({ commit }, page) {
        commit('CHANGECOURSEPAGE', page);
    },
    changeallcoursepage({commit},page){
        commit("CHANGEALLCOURSEPAGE",page);
    },
    async getcoursebyname({ commit }, data) {
        let res = await http.getcoursebyname({ name: data });
        if (res.status == 200) {
            commit('GETCOURSEBYNAME', res.data)
        }
    },
    async getstuacc({ commit }) {
        let res = await http.getstuacc();
        if (res.status == 200) {
            commit("GETSTUACC", res.data);
        }
    },
    changestuaccpage({ commit }, page) {
        commit("CHANGESTUACCPAGE", page);
    },
    deletestuacc({commit},index){
        commit("DELETESTUACC",index);
    },
    async getstubyname({commit},data){
        let res=await http.getstubyname({name:data});
        if(res.status==200){
            commit("GETSTUBYNAME",res.data);
        }
    },
    async getallcourse({commit}){
        let res=await http.getallcourse();
        if(res.status==200){
            commit("GETALLCOURSE",res.data);
        }
    },
    deleteallcourse({commit},index){
        commit("DELETEALLCOURSE",index);
    },
    async getallcoursebyname({commit},data){
        let res=await http.getallcoursebyname({coursename:data})
        if(res.status==200){
            commit("GETALLCOURSEBYNAME",res.data);
        }
    }
};
const mutations = {
    GETPUBINFO(state, pub) {
        state.pubinfo = pub;
    },
    GETACCOUNT(state, data) {
        state.getaccountdata = data;
    },
    DELETEACCOUNT(state, index) {
        state.getaccountdata.tabledata.splice((state.getaccountdata.currentpage - 1) * state.getaccountdata.pagesize + index, 1);
        state.getaccountdata.total -= 1;
        if (state.getaccountdata.pagesize == 1) {
            if (state.getaccountdata.currentpage != 1) {
                state.getaccountdata.currentpage -= 1;
            }
        }
    },
    CHANGEACCOUNTPAGE(state, page) {
        state.getaccountdata.currentpage = page;
    },
    GETTCHACCBYNAME(state, data) {
        state.getaccountdata = data;
    },
    GETCOURSE(state, data) {
        state.getcoursedata = data;
    },
    DELETECOURSE(state, index) {
        state.getcoursedata.tabledata.splice((state.getcoursedata.currentpage - 1) * state.getcoursedata.pagesize + index, 1);
        state.getcoursedata.total -= 1;
        if (state.getcoursedata.pagesize == 1) {
            if (state.getcoursedata.currentpage != 1) {
                state.getcoursedata.currentpage -= 1;
            }
        }
    },
    DELETEALLCOURSE(state,index){
        state.getallcourse.tabledata.splice((state.getallcourse.currentpage - 1) * state.getallcourse.pagesize + index, 1);
        state.getallcourse.total -= 1;
        if (state.getallcourse.pagesize == 1) {
            if (state.getallcourse.currentpage != 1) {
                state.getallcourse.currentpage -= 1;
            }
        }
    },
    CHANGECOURSEPAGE(state, page) {
        state.getcoursedata.currentpage = page;
    },
    GETCOURSEBYNAME(state, data) {
        state.getcoursedata = data;
    },
    GETSTUACC(state, data) {
        state.getstuaccdata = data;
    },
    CHANGESTUACCPAGE(state, page) {
        state.getstuaccdata.currentpage = page;
    },
    DELETESTUACC(state,index){
        state.getstuaccdata.tabledata.splice((state.getstuaccdata.currentpage - 1) * state.getstuaccdata.pagesize + index, 1);
        state.getstuaccdata.total -= 1;
        if (state.getstuaccdata.pagesize == 1) {
            if (state.getstuaccdata.currentpage != 1) {
                state.getstuaccdata.currentpage -= 1;
            }
        }
    },
    GETSTUBYNAME(state,data){
        state.getstuaccdata=data;
    },
    GETALLCOURSE(state,data){
        state.getallcourse=data;
    },
    CHANGEALLCOURSEPAGE(state,page){
        state.getallcourse.currentpage=page;
    },
    GETALLCOURSEBYNAME(state,data){
        state.getallcourse=data;
    }
};
const getters = {};
export default {
    state,
    actions,
    mutations,
    getters
}